package cn.org.spring.wechar.bean;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.HashMap;

@Getter
@Setter
public class AccessTokenBean implements Serializable {
    //固定值，官方文档的
    private String grant_type = "client_credential";
    //用户唯一标识
    private String appId;
    //用户唯一凭证的秘钥
    private String secret;
    //获取到access的token
    private String accessToken;
    //获取access过期时间
    private long expiresTime;


    private AccessTokenBean(String appId, String secret, String accessToken, long expiresTime) {
        this.appId = appId;
        this.secret = secret;
        this.accessToken = accessToken;
        if (expiresTime > 0){
            this.expiresTime = System.currentTimeMillis() + expiresTime * 1000;
        }
    }

    /**
     * 构建获取access token 请求对象
     * @param appId
     * @param secret
     * @return
     */
    public static HashMap<String,String> requestOf(String appId,String secret){
        HashMap<String,String> request = new HashMap<>(16);
        request.put("appId",appId);
        request.put("secret",secret);
        request.put("grant_type","client_credential");
        return request;
    }

    /**
     * 构建AccessToken对象
     * @param accessToken
     * @param expiresTime
     * @return
     */
    public static AccessTokenBean responseOf(String accessToken,long expiresTime){
        return new AccessTokenBean("","",accessToken,expiresTime);
    }

    /**
     * 判断token是否已经失效
     * @return
     */
    public boolean isExpiresTime(){
        return System.currentTimeMillis() > expiresTime;
    }
}
